import type {
  ComputeContext,
  CreateCrudOptionsProps,
  CreateCrudOptionsRet,
} from '@fast-crud/fast-crud';

import type { Recordable } from '@vben/types';

import { compute, dict } from '@fast-crud/fast-crud';
import { cloneDeep } from 'lodash-es';

import { findOnlineService } from '#/api';
import { DictEnum } from '#/enums/commonEnum';
import { backendDict } from '#/plugins/fast-crud/common';

export function createCrudOptions(
  props: CreateCrudOptionsProps,
): CreateCrudOptionsRet {
  function emitJson() {
    const { emit } = props.context;
    const list: Recordable[] = props.crudExpose.getTableData();
    emit('update:modelValue', list);
  }
  return {
    crudOptions: {
      search: { show: false },
      toolbar: { show: false },
      pagination: { show: false },
      container: {
        is: 'fs-layout-default',
      },
      mode: {
        name: 'local',
        isMergeWhenUpdate: true,
        isAppendWhenAdd: true,
      },
      form: {
        col: { span: 24 },
        afterSubmit: emitJson,
      },
      table: {
        rowKey: (row: any) => row.tempId,
        striped: true,
        remove: {
          afterRemove: emitJson,
        },
      },
      actionbar: {
        buttons: {
          add: { show: props.context.mode !== 'view' },
          select: {
            show: props.context.mode !== 'view',
            text: '选择',
            type: 'primary',
            click() {
              const list: Recordable[] = props.crudExpose.getTableData();
              // props.context.openModal(true, { selectedData: cloneDeep(list) });
              props.context.apiSelectRef.value.setData({
                selectedData: cloneDeep(list),
              });
              props.context.apiSelectRef.value.open();
            },
          },
        },
      },
      rowHandle: {
        width: 120,
        buttons: {
          edit: {
            show: compute((context: ComputeContext) => {
              return props.context.mode !== 'view' && context.row.isInput;
            }),
          },
          view: { show: false },
          copy: { show: false },
          remove: {
            show: compute(() => {
              return props.context.mode !== 'view';
            }),
          },
        },
      },
      columns: {
        isInput: {
          title: '是否手动录入',
          type: 'text',
          column: { show: false },
          form: { show: false },
        },
        springApplicationName: {
          title: '服务名',
          type: 'dict-select',
          dict: dict({
            async getData() {
              return await findOnlineService();
            },
          }),
          form: {
            helper: {
              position: 'label',
              text: 'lamp-cloud: 自动查询后台已经正常启动并注册到nacos中的服务 \nlamp-boot: 后台拆分的模块',
            },
            rules: { required: true, message: '这是必填项' },
          },
          column: { show: false },
        },
        controller: {
          title: '控制器类名',
          type: 'text',
          form: {
            rules: { required: true, message: '这是必填项' },
          },
          column: { show: false },
        },
        name: {
          title: '接口名称',
          type: 'text',
          form: {
            rules: { required: true, message: '这是必填项' },
          },
        },
        uri: {
          title: '接口地址',
          type: 'text',
          form: {
            rules: { required: true, message: '这是必填项' },
          },
          column: {
            width: 250,
            ellipsis: true,
          },
        },
        requestMethod: {
          title: '请求类型',
          type: 'dict-select',
          dict: backendDict({ type: DictEnum.HttpMethod }),
          form: {
            rules: { required: true, message: '这是必填项' },
          },
          column: { show: false },
        },
      },
    },
  };
}
